package com.yanqu.road.dao.impl.kuang.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.kuang.config.KuangEquipmentConfig;
import com.yanqu.road.entity.kuang.config.KuangPlaceConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class KuangConfigDaoImpl extends TempDao {

    private static String getKuangEquipmentConfigSql = "select * from `t_p_kuang_equipment` ORDER BY `id` ASC";

    public Map<Integer, KuangEquipmentConfig> getKuangEquipmentConfigMap() {
        Map<Integer, KuangEquipmentConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getKuangEquipmentConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                KuangEquipmentConfig kuangEquipmentConfig = new KuangEquipmentConfig();
                kuangEquipmentConfig.setId(rs.getInt("id"));
                kuangEquipmentConfig.setType(rs.getInt("type"));
                kuangEquipmentConfig.setQuality(rs.getInt("quality"));
                kuangEquipmentConfig.setAttribute(rs.getString("attribute"));
                kuangEquipmentConfig.setAdvancedAttribute(rs.getString("advancedAttribute"));
                kuangEquipmentConfig.setFixAttribute(rs.getString("fixAttribute"));
                map.put(kuangEquipmentConfig.getId(), kuangEquipmentConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getKuangPlaceConfigSql = "select * from `t_p_kuang_place` ORDER BY `id` ASC";

    public Map<Integer, KuangPlaceConfig> getKuangPlaceConfigMap() {
        Map<Integer, KuangPlaceConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getKuangPlaceConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                KuangPlaceConfig placeConfig = new KuangPlaceConfig();
                placeConfig.setId(rs.getInt("id"));
                placeConfig.setType(rs.getInt("type"));
                placeConfig.setLevel(rs.getInt("level"));
                placeConfig.setRadius(rs.getInt("radius"));
                placeConfig.setSeizeScore(rs.getInt("seizeScore"));
                placeConfig.setSeizeProduce(rs.getInt("seizeProduce"));
                map.put(placeConfig.getId(), placeConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
